package com.psy.counseling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.psy.counseling.entity.Comment;
import com.psy.counseling.entity.User;
import com.psy.counseling.mapper.CommentMapper;
import com.psy.counseling.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psy.counseling.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  寻找父评论 
 * </p>
 *
 * @author 梁学儒
 * @since 2022-05-31
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Resource
    private UserService userService;

    public List<Comment> findByForeign() {
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.<Comment>lambdaQuery().eq(Comment::getForeignId, 0).orderByDesc(Comment::getId);
        List<Comment> list = list(queryWrapper);
        for (Comment comment : list) {
            User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, comment.getUsername()));
    /*        Message.setAvatar("http://localhost:9999/files/" + one.getAvatar());*/
            Long parentId = comment.getParentId();
            list.stream().filter(c -> c.getId().equals(parentId)).findFirst().ifPresent(comment::setParentComment);
        }
        return list;
    }
}
